/**
 * AssessChangePanel
 * @Author Ace.c
 * @Create 2016-12-28 15:53
 */
class AssessChangePanel extends BasePanel {

    private group: eui.Group;
    private item_1: AssessPlayerItem;
    private item_2: AssessPlayerItem;
    private item_3: AssessPlayerItem;
    private btn_confirm: eui.Button;

    private list: any[] = [];

    constructor() {
        super();

        this.skinName = "AssessChangePanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.item_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.item_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.item_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        GameSound.play('sound_btn_click');

        switch (e.currentTarget) {
            case this.btn_confirm:
                let appraise: any = {};
                for (let i: number = 0; i < this.list.length; i++) {
                    appraise[this.list[i]] = this["item_" + (i + 1)].comment;
                }

                if (GSController.i.gsTitleView) GSController.i.gsTitleView.btn_assess.visible = false;

                this.hide();

                gameManager.i.dispatchEvent(GameEventType.UPDATE_ASSESS_DATA, appraise);

                gameManager.socketManager.send(29, {
                    id: this.data.id,
                    appraise: appraise
                });
                break;
            case this.item_1:
                this.item_1.commentB = this.item_1.comment == 1 ? 0 : 1;
                this.item_1.head.src = this.item_1.comment == 1 ? "assess" : "unassess";
                break;
            case this.item_2:
                this.item_2.commentB = this.item_2.comment == 1 ? 0 : 1;
                this.item_2.head.src = this.item_2.comment == 1 ? "assess" : "unassess";
                break;
            case this.item_3:
                this.item_3.commentB = this.item_3.comment == 1 ? 0 : 1;
                this.item_3.head.src = this.item_3.comment == 1 ? "assess" : "unassess";
                break;
            default:
                break;
        }
    }

    update(data: any) {

        this.data = data;

        let item: AssessPlayerItem;
        for (let i: number = 1; i <= 3; i++) {
            item = this['item_' + i];
            item.clean();

            if (this.group.contains(item)) this.group.removeChild(item);
        }

        this.list = [];

        let index: number = 1;
        let player: any;
        for (let key in data.data) {
            player = data.data[key];

            item = this['item_' + index];
            item.update(player);
            item.commentB = player.zan;

            this.list.push(player.uid);

            if (!this.group.contains(item)) this.group.addChild(item);

            index++;
        }
    }
}